﻿#include "Viewport.h"
#include "GameDeviceSingleton.h"

CViewPort::CViewPort()
{
	//CurView = CRect();
}

CViewPort::CViewPort(double X, double Y, int Width, int Height)
{
	this->_data = new ViewPortData();
	this->_data->X = X;
	this->_data->Y = Y;
	this->_data->Width = Width;
	this->_data->Height = Height;

	_data->CurView = new Rect(this->_data->X + 800/2, this->_data->Y  + 600/2, this->_data->Width, this->_data->Height);
	_data->quadTreeView = new Rect(0,0, 1000, 1000);
	viewportupdatehorizontal = new ViewPortUpdateHorizontal(this->_data);
	viewportupdatevertical = new ViewPortUpdateVertical(this->_data);
	viewportupdate = viewportupdatehorizontal;
}

Rect* CViewPort::getQuadTreeView() {
	_data->quadTreeView->X = _data->CurView->X;
	_data->quadTreeView->Y = _data->CurView->Y;
	return _data->quadTreeView;
}

void CViewPort::setPosition(double x, double y) {
	_data->X = x - 200;
	_data->Y = y - 400;
	_data->CurView->setPosition(_data->X + this->_data->Width/2, _data->Y + this->_data->Height/2);
}

void CViewPort::setTarget(Rect* targetRect) {
	this->_data->targetRect = targetRect;
}

void CViewPort::update() {
	viewportupdate->update(GameDeviceSingleton::getInstance()->getMapWidth(), GameDeviceSingleton::getInstance()->getMapHeight());
}

void CViewPort::setViewPort(bool isHorizontal) {
	if (isHorizontal) {
		viewportupdate = viewportupdatehorizontal;
	} else {
		viewportupdate = viewportupdatevertical;
	}
}

void CViewPort::Move(D3DXVECTOR2 NewPosition, int MapWidth, int MapHeight)
{
	D3DXVECTOR2 delta = D3DXVECTOR2((float)NewPosition.x - _data->X, (float)NewPosition.y - _data->Y);
	if(NewPosition.x >= 0)
	{
		if(delta.x + _data->X + _data->Width <= MapWidth)
		{
			_data->X = _data->X + delta.x;
		}
		else
		{
			_data->X = MapWidth - _data->Width;
		}
	}
	else
	{
		if(delta.x + _data->X >= 0)
		{
			_data->X = delta.x + _data->X;
		}
		else
		{
			_data->X = 0;
		}
	}

	if(NewPosition.y >= 0)
	{
		if(_data->Y + delta.y <= MapHeight)
		{
			_data->Y += delta.y;
		}
		else
		{
			_data->Y = MapHeight;
		}
	}
	else
	{
		if(_data->Y + delta.y >= 0)
		{
			_data->Y += delta.y;
		}
		else
		{
			_data->Y = 0;
		}
	}

	_data->CurView->setPosition(_data->X + this->_data->Width/2, _data->Y + this->_data->Height/2);

}

void CViewPort::updateSettings(double heightLimit, double widthLimit, double delteToMoveHeight) {
	this->_data->heightLimit = heightLimit;
	this->_data->widthLimit = widthLimit;
	this->_data->deltaToMoveHeight = delteToMoveHeight;
}


double CViewPort::getLeft()
{
	return _data->X;
}


double CViewPort::getright()
{
	return _data->X + _data->Width;
}

double CViewPort::getTop()
{
	return _data->Y;
}

double CViewPort::getBottom()
{
	return _data->Y + _data->Height;
}
